package com.mt.roll.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.ReportSheet;
import com.mt.roll.core.dal.service.ReportSheetServiceBiz;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.report.params.ReportSheetParam;
import com.mt.roll.report.params.ReportSheetQueryParam;
import com.mt.roll.report.service.ReportSheetService;
import com.mt.roll.report.vo.ReportSheetVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: liguanghao
 * @Date: 2021/3/1 14:14
 * @Version 1.0
 */
@Service
@Transactional
public class ReportSheetServiceImpl extends BaseService implements ReportSheetService {

    @Resource
    private ReportSheetServiceBiz reportSheetServiceBiz;
    @Override
    public R<ReportSheetVo> get(IdParam idParam) {

        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        ReportSheet reportSheet = reportSheetServiceBiz.getById(idParam.getId());
        ReportSheetVo reportSheetVo = ReportSheetVo.builder().build();
        BeanUtils.copyProperties(reportSheet, reportSheetVo);
        return successful(reportSheetVo);
    }

    @Override
    public R<List<ReportSheetVo>> list(ReportSheetQueryParam reportSheetQueryParam) {
        QueryWrapper<ReportSheet> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(reportSheetQueryParam.getName()), ReportSheet.NAME, reportSheetQueryParam.getName());
        List<ReportSheet> list = reportSheetServiceBiz.list(wrapper);
        List<ReportSheetVo> reportSheetVos = com.mt.roll.core.utils.BeanUtils.copyList(list, ReportSheetVo.class);
        return successful(reportSheetVos);
    }

    @Override
    public R<PageVo<ReportSheetVo>> page(ReportSheetQueryParam reportSheetQueryParam) {
        QueryWrapper<ReportSheet> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(reportSheetQueryParam.getName()), ReportSheet.NAME, reportSheetQueryParam.getName());
        wrapper.eq(reportSheetQueryParam.getCategory()!=null,ReportSheet.CATEGORY,reportSheetQueryParam.getCategory());
        Page<ReportSheet> page = reportSheetServiceBiz.page(new Page<>(reportSheetQueryParam.getCurrent(), reportSheetQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, ReportSheetVo.class));
    }

    @Override
    public R<IdVo> add(ReportSheetParam reportSheetParam) {
        QueryWrapper<ReportSheet> wrapper = new QueryWrapper<>();
        wrapper.eq(ReportSheet.NAME, reportSheetParam.getName());
        Assert.eqZero(reportSheetServiceBiz.count(wrapper), "名称已存在");
        ReportSheet reportSheet=new ReportSheet();
        BeanUtils.copyProperties(reportSheetParam, reportSheet);
        setCommonField(reportSheet);
        reportSheetServiceBiz.save(reportSheet);
        return successful(IdVo.builder().id(reportSheet.getId()).build());
    }

    @Override
    public R<IdVo> update(ReportSheetParam reportSheetParam) {
        ReportSheet reportSheet = reportSheetServiceBiz.getById(reportSheetParam.getId());
        if (!reportSheet.getName().equals(reportSheet.getName())) {
            QueryWrapper<ReportSheet> wrapper = new QueryWrapper<>();
            wrapper.eq(ReportSheet.NAME, reportSheetParam.getName());
            Assert.eqZero(reportSheetServiceBiz.count(wrapper), "名称已存在");
        }
        ReportSheet updateReportSheet = new ReportSheet();
        BeanUtils.copyProperties(reportSheetParam, updateReportSheet);
        setUpdateCommonField(updateReportSheet);
        updateReportSheet.setName(updateReportSheet.getFileName().concat(".ureport.xml"));
        reportSheetServiceBiz.updateById(updateReportSheet);
        return successful(IdVo.builder().id(reportSheet.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        ReportSheet reportSheet = reportSheetServiceBiz.getById(idParam.getId());
        if (reportSheet == null) {
            return failed("报表不存在");
        }
        reportSheetServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(reportSheet.getId()).build());
    }

    @Override
    public ReportSheet getReportByName(String correctName) {
        QueryWrapper<ReportSheet> wrapper = new QueryWrapper();
        wrapper.eq(ReportSheet.NAME, correctName);
        return reportSheetServiceBiz.getOne(wrapper);
    }

    @Override
    public void deleteReportByName(String correctName) {
        QueryWrapper<ReportSheet> wrapper = new QueryWrapper();
        wrapper.eq(ReportSheet.NAME, correctName);
        reportSheetServiceBiz.remove(wrapper);
    }
}
